﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XSTDZ.ERP.Application;
using XSTDZ.ERP.Application.DataContracts;
using XSTDZ.ERP.Infrastructure;
using XSTDZ.ERP.Infrastructure.Authentication;
using XSTDZ.ERP.WebApp.ViewModel;
using XSTDZ.ERP.WebApp.Models;
using XSTDZ.ERP.Infrastructure.Extentions;
namespace XSTDZ.ERP.WebApp.Controllers
{
    [NoAuthorizated]
    public class OrgController : Controller
    {
        private readonly IOrgService orgServiceImpl = ServiceLocator.Instance.GetService<IOrgService>();	
        
        //新增
        public ActionResult Index(string id,string orgname)
        {
            ViewBag.ParentId = id??"";
            ViewBag.ParentName = orgname ?? "";
            return View();
        }
        [HttpPost]

        public ActionResult Index(OrgDto org)
        {
            if (ModelState.IsValid)
            {
               
                var id = orgServiceImpl.AddOrg(org);
                ViewBag.PostState = 9;
                ViewBag.AddId = id;
            }
            return View();
        }

        [HttpGet]
        public ActionResult Edit(string id)
        {
            if (id.IsEmpty())
            {
               return null;
            }
            var orgKey=Guid.Parse(id);
            var org=orgServiceImpl.GetOrg(orgKey);
            return View(org);
        }
        [HttpPost]
        public ActionResult Edit(OrgDto org)
        {
            if (ModelState.IsValid)
            {
                orgServiceImpl.UpdateOrg(org);
                ViewBag.PostState = 9;
            }
            return View(org);
        }
        [HttpGet]
        public ActionResult Delete(string id)
        {
            orgServiceImpl.DeleteOrg(Guid.Parse(id));
            return Content("ok");
        }
      
        //组织机构树列表
        [HttpGet]
        public ActionResult List()
        {
            return View();
        }
        //添加用户时选择组织机构(默认只选择部门 -1:全部 0:部门 1:组织机构)
        [HttpGet]
        public ActionResult SelectOrgTree(int? selectType)
        {
            var type = 0;
            if (selectType != null)
            {
                Int32.TryParse(selectType.ToString(), out type);
            }
            ViewBag.SelectType = type;
            return View();
        }
        [NoAuthorizated("TreeDate")]
        public JsonResult TreeDate()
        {
            var org = orgServiceImpl.GetAllOrg().OrderByDescending(t=>t.Order).ToList();
            var dept = orgServiceImpl.GetAllDept().OrderByDescending(t=>t.Order).ToList();
            if (org == null||org.Count==0)//初始化一个根结点
            {
                var orgroot = new OrgModel().InitOrgRoot();
                orgServiceImpl.AddOrg(orgroot);
                org = orgServiceImpl.GetAllOrg().OrderByDescending(t => t.Order).ToList();
            }
            var orgViewModel = new OrgModel().GetAllOrgDept(org, dept);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            return Json(orgViewModel, JsonRequestBehavior.AllowGet);
        }
    }
}
